﻿using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Models.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsORM;
using UtilsORM.MySqlData;

namespace ServiceWeb.DBGateway
{
    public class DBIotDeviceModelVariableRelation
    {

        EFYihongIotContext ef = new EFYihongIotContext();



        public bool saveAll(IFormCollection FC, HttpReqModel httpRequest, MagerLoginInfo LoginUserInfo, iot_device_model_variable_relation req)
        {
            //先查一下有没重复提交
            iot_device_model_variable_relation model = ef.iot_device_model_variable_relation.Where(c => c.mv_guid == req.mv_guid).FirstOrDefault();
            if (model == null)
            {
                //查编码

                //写入
                req.mv_name = ef.iot_device_model_variable.Where(c => c.v_guid == req.mv_v_guid).FirstOrDefault().v_name;
                req.mv_guid = Guid.NewGuid().ToString();
                req.mv_add_time = DateTime.Now;
                req.mv_guid = Guid.NewGuid().ToString();
                ef.iot_device_model_variable_relation.Add(req);
                ef.SaveChanges();
            }
            else
            { //  1执行器，2传感器，3网关,4延时
                model.mv_name = ef.iot_device_model_variable.Where(c => c.v_guid == req.mv_v_guid).FirstOrDefault().v_name;
                model.mv_model_guid = req.mv_model_guid;
                model.mv_limit = req.mv_limit;
                model.mv_add_time = DateTime.Now;
                ef.iot_device_model_variable_relation.Update(model);
                ef.SaveChanges();
            }
            return true;
        }

        public List<Dictionary<string, string>> getEditRelationList(string cguid, HttpReqModel httpReqModel, string operation)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            List<iot_device_model_variable_relation> variable_Relations = ef.iot_device_model_variable_relation.Where(c => c.mv_model_guid == httpReqModel.modelguid).ToList();

            List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();


            foreach (iot_device_model_variable_relation item in variable_Relations)
            {
                Dictionary<string, string> directory = new Dictionary<string, string>();

                iot_device_model_variable variable = ef.iot_device_model_variable.Where(c => c.v_guid == item.mv_v_guid).FirstOrDefault();
                directory.Add("mv_guid", item.mv_guid);
                if (variable != null)
                {
                    directory.Add("name", variable.v_name);
                    directory.Add("key", variable.v_key);
                }
                directory.Add("mv_limit", item.mv_limit);
                list.Add(directory);
            }
            return list;
        }

        public iot_device_model_variable_relation getVariableRelation(string mvguid)
        {
            return ef.iot_device_model_variable_relation.Where(c => c.mv_guid == mvguid).FirstOrDefault();
        }
    }
}
